﻿using System;
using System.ComponentModel;
using System.Linq;
using Inaction.Core;
using Inaction.Mapping;
using System.ComponentModel.DataAnnotations;

namespace Inaction.Business.Security
{
    [Table("Assembly"), DisplayName("程序集"), Serializable]
    public class Assembly : Abstract.Assembly<Assembly>
    {
        #region 子表

        [Display(Description = "程序集类"), DisplayName("程序集类")]
        public AssemblyClassList AssemblyClasss
        {
            get { return GetChild<AssemblyClassList, AssemblyClass>(); }
        }

        #endregion
    }

    [DisplayName("程序集列表"), Serializable]
    public class AssemblyList : Inaction.Business.EntityList<AssemblyList, Assembly>
    {
        protected override void OnSaving()
        {
            base.OnSaving();
            foreach (var assembly in this)
            {
                foreach (var assemblyClass in assembly.AssemblyClasss)
                {
                    foreach (var resource in assemblyClass.AssemblyClassResources)
                    {
                        for (int i = 0; i < resource.AssemblyClassResourceRoles.Count; i++)
                        {
                            var resourceRole = resource.AssemblyClassResourceRoles[i];
                            if (assemblyClass.ClassType == Rules.ClassType.Entity)
                            {
                                if (resourceRole.AllowSet == true && resourceRole.AllowGet == true)
                                {
                                    resource.AssemblyClassResourceRoles.RemoveAt(i);
                                    i--;
                                }
                            }
                            else if (assemblyClass.ClassType == Rules.ClassType.View)
                            {
                                if (resourceRole.AllowExecute == true)
                                {
                                    resource.AssemblyClassResourceRoles.RemoveAt(i);
                                    i--;
                                }
                            }
                        }
                    }
                    for (int j = 0; j < assemblyClass.AssemblyClassRoles.Count; j++)
                    {
                        var classRole = assemblyClass.AssemblyClassRoles[j];
                        if (classRole.AllowEdit == classRole.AllowNew == classRole.AllowQuery == classRole.AllowRemove == true)
                        {
                            assemblyClass.AssemblyClassRoles.RemoveAt(j);
                            j--;
                        }
                    }
                }
            }
        }
    }
}
